class Observer{
    constructor(data) {
        this.walk(data)
    }
    
    // 
    walk(data) {
        if(!data || typeof data !== 'object') return
        Object.keys(data).forEach(key => {
            this.defineReactive(data,key,data[key])
        })
    }

    defineReactive(obj, key, val) {
        let that = this
        // 负责收集依赖并发送通知
        let dep = new Dep()
        this.walk(val)
        Object.defineProperty(obj, key, {
            enumerable: true,
            configurable: true,
            get() {
                // 依赖收集
                Dep.target && dep.addSubs(Dep.target)
                return val
            },
            set(newValue) {
                // 
                if (newValue === val) return
                val = newValue
                that.walk(newValue)
                // 发送通知
                dep.notify()
            }
        })
    }
}